# -*- coding: UTF-8 -*-
# 安装MYSQL DB for python
import MySQLdb as mdb
import redis
import datetime

con = None
redis_user = '10.13.16.202'
mysql_blued = '10.13.27.242'
mysql_qa = '10.9.128.47'
mysql_chandao = '117.79.87.29'


class PyRedis:
    def __init__(self, host):
        self.host = host
        self.port = 6379
        self.r = redis.StrictRedis(host=self.host, port=self.port)


# 测试环境下添加测试用户
def add(v1, v2, v3, v5):
    try:
        # print v1, v2, v3, v5
        con = mdb.connect(mysql_qa, 'root', 'qatest', 'qa', charset='utf8');
        with con:
            cur = con.cursor()
            # 执行一个查询
            state = cur.execute(
                "insert INTO qa_load(username, password, type, uid) VALUES ('%s', '%s', %d, '%s')" % (v1, v2, v3, v5))
            # 取得上个查询的结果，是单个结果
            result = cur.fetchone()
            return result, state
    finally:
        con.close()


def sel(num):
    try:
        con = mdb.connect(mysql_qa, 'root', 'qatest', 'qa', charset='utf8');
        with con:
            cur = con.cursor()
            # 执行一个查询
            cur.execute("select username,password FROM qa_load WHERE type = 0")
            # 取得上个查询的结果，是单个结果
            result = cur.fetchall()[0:num]
            # print result
            return result
    finally:
        con.close()
